Complete the solution so that it splits the string into pairs of two characters. If the >string contains an odd number of characters then it should replace the missing second >character of the final pair with an underscore ('_').
Examples:
- 'abc' => ['ab', 'c_']
- 'abcdef' => ['ab', 'cd', 'ef']
題目理解:將字串s分成每兩個字元一對,若字串s長度為奇數,則用_來替換最後一組中缺少的字元。
def solution(s):
"""將字串每兩字元做拆分"""
lst =[]
if len(s)%2 == 1:
s = s +"_"
for x in range(0,len(s),2):
lst.append(s[x:x+2])
return lst